<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/22
 * Time: 下午5:12
 */

namespace App\Http\Services;


use App\Http\Model\Gift;
use App\Http\Model\Mall;
use App\Http\Model\Room;
use App\Http\Model\RoomHostMicro;
use App\Http\Model\RoomUser;
use App\Http\Model\User;
use App\Http\Model\Hint;
use App\Http\Model\RoomMicro;
use App\Http\Model\RoomWaitbit;
use const Couchbase\HAVE_IGBINARY;

class RoomHostMicroServices extends CommonServices
{
    protected $model_room_user;
    protected $model_user;
    protected $model_room;
    protected $model_room_hostmicro;
    protected $model_room_waitbit;
    protected $model_room_micro;
    protected $hostmicro_lock_key = "micro";
    protected $model_mall;
    protected $model_gift;

    public function __construct()
    {
        $this->model_room_user = new RoomUser();
        $this->model_user = new User();
        $this->model_room = new Room();
        $this->model_room_hostmicro = new RoomHostMicro();
        $this->model_room_waitbit = new RoomWaitbit();
        $this->model_room_micro = new RoomMicro();
        $this->model_mall = new Mall();
        $this->model_gift = new Gift();
    }

    /**
     * 获取麦克风信息
     * @param $room_id
     * @return array|int
     */
    public function get_micro($room_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)) {
            return Hint::LOGIN_OUT;
        }

        $user_id = $this->model_room_hostmicro->get_user_id($room_id);
        $volume = $this->model_room_hostmicro->get_volume($room_id);
        if ($user_id == null){
            return [
                "microphone_id" => 0,
                "volume" => $volume,
                "user" => null
            ];
        }

        $user = $this->model_user->get_user($user_id);
        $user['attires'] = $this->model_mall->getGoodsAttire($user_id);

        $gift = $this->model_room_hostmicro->get_gift($room_id);
        if (!empty($gift)) {//重写一下key名,app端保持统一
            $gift['gift_name'] = $gift['name'];
            $gift['gift_image'] = $gift['img'];
            $gift['gift_resource'] = '';
            unset($gift['name']);
            unset($gift['img']);
            unset($gift['price']);
        }

        return [
            "microphone_id" => 0,
            "volume" => $volume,
            "user" => $user,
            "gift" => $gift,
        ];
    }


    /**
     * 设置主持人上麦 [需要改造，依赖于三方平台CALLBACK]
     * @param $room_id
     * @return int
     */
    public function into($room_id, &$user_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $user_id = $current_user['user_id'];
        if ($this->model_room->get_role($room_id,$user_id) == 0){
            return Hint::AUTH_ERROR;
        }
        if (!$this->model_room_user->exists($room_id, $user_id)){
            return Hint::ROOM_USER_NOT_EXIST;
        }
        $micro_user_id = $this->model_room_hostmicro->get_user_id($room_id);
        if ($micro_user_id != null){
            return Hint::ROOM_MICRO_NOT_NULL;
        }

        if ($this->model_room_waitbit->get_guard_type($room_id, $user_id) !== null){
            return Hint::ROOM_WAITBIT_IS_EXIST;
        }
        if ($this->model_room_micro->get_user_micro_status($room_id, $user_id) !== null){
            return Hint::ROOM_MICRO_USER_IS_EXIST;
        }
        if ($this->model_room_hostmicro->get_user_status($room_id, $user_id) !== null){
            return Hint::ROOM_MICRO_USER_IS_EXIST;
        }
        if (!redis_lock($this->hostmicro_lock_key . $room_id, 5)){
            return Hint::NOT_ALLOWED;
        }
        $user_id = $current_user["user_id"];
        //$this->model_room_hostmicro->set_user_id($room_id, $user_id);
        //$this->model_room_hostmicro->set_user_status($room_id, $user_id, 0);
        $this->model_room_hostmicro->set_into_begin($room_id, $user_id);
        return true;
    }


    /**
     * 主持麦下麦
     * @param $room_id
     * @return bool|int
     */
    public function out($room_id, &$user_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $current_user_id = $current_user['user_id'];
        $micro_user_id = $this->model_room_hostmicro->get_user_id($room_id);
        if ($current_user_id != intval($micro_user_id)){
            return Hint::ROOM_USER_NOT_EXIST;
        }
        if (!redis_lock($this->hostmicro_lock_key . $room_id, 5)){
            return Hint::NOT_ALLOWED;
        }
        $user_id = $current_user_id;
        //$this->model_room_hostmicro->del_user_id($room_id);
        return true;
    }



    /**
     * 开麦
     * @param $room_id
     * @return bool|int
     */
    public function volume_open($room_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $current_user_id = $current_user['user_id'];
        $micro_user_id = $this->model_room_hostmicro->get_user_id($room_id);
        if ($current_user_id != intval($micro_user_id)){
            return Hint::AUTH_ERROR;
        }
        //检查禁麦
        $blockMicro = $this->model_user->getMicroBlockStatus($current_user_id);
        if ($blockMicro == true){
            return Hint::USER_MICRO_IS_BLOCK;
        }

        $this->model_room_hostmicro->set_volume_open($room_id);
        return true;
    }

    /**
     * 闭麦
     * @param $room_id
     * @return bool|int
     */
    public function volume_close($room_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $current_user_id = $current_user['user_id'];
        $micro_user_id = $this->model_room_hostmicro->get_user_id($room_id);
        if ($current_user_id != intval($micro_user_id)){
            return Hint::AUTH_ERROR;
        }
        $this->model_room_hostmicro->set_volume_close($room_id);
        return true;
    }


    /**
     * 语音平台返回下麦消息
     * @param $room_id
     * @param $user_id
     * @param $timestamp
     * @return bool
     */
    public function callback_out($room_id, $user_id, &$timestamp){
        $stauts = $this->model_room_hostmicro->get_user_status($room_id, $user_id);
        if ($stauts === null){
            return false;
        }
        $this->model_room_hostmicro->del_user_status($room_id, $user_id);
        $this->model_room_hostmicro->del_user_id($room_id);
        //$this->model_room_hostmicro->set_into_begin($room_id, $user_id);
        $timestamp = time() + 9999999;
        $this->model_room->set_close_timestamp($room_id, $timestamp);
        $this->model_room->add_wait_close($room_id);

        redis_unlock($this->hostmicro_lock_key.$room_id);
        return true;
    }


    /**
     * 语音平台返回上麦消息
     * @param $room_id
     * @param $user_id
     * @return bool
     */
    public function callback_into($room_id, $user_id){
        $micro_id = $this->model_room_hostmicro->get_into_begin($room_id, $user_id);
        if ($micro_id === null){
            return false;
        }
        $this->model_room_hostmicro->set_user_status($room_id, $user_id, 1);
        $this->model_room_hostmicro->set_user_id($room_id, $user_id);
        $this->model_room_hostmicro->del_into_begin($room_id, $user_id);

        $this->model_room->del_close_timestamp($room_id);
        $this->model_room->set_opened($room_id);

        redis_unlock($this->hostmicro_lock_key . $room_id);
        return true;
    }


    /**
     * 标记礼物数据
     * @param $room_id
     * @param $user_id
     */
    public function mark_gift($room_id, $send_user_id, $user_id, $gift_id, $gift_num){
        $status = $this->model_room_hostmicro->get_user_status($room_id, $user_id);
        if (empty($status)){
            return false;
        }
        $old_gift_data = $this->model_room_hostmicro->get_gift($room_id);
        $user = $this->model_user->get_user($send_user_id);
        if (empty($user)){
            return false;
        }
        $user_image = null;
        if (count($user["images"]) > 0) {
            $user_image = $user["images"][0];
        }
        $gift = $this->model_gift->get_gift($gift_id);
        if (empty($old_gift_data)){
            $this->model_room_hostmicro->set_gift($room_id, $user['user_id'], $user['nickname'], $user_image, $gift_id, $gift['name'], $gift['image'], $gift_num, intval($gift['price']));
            return true;
        }
        $old_price = intval($old_gift_data['price']);
        $old_num = intval($old_gift_data['num']);
        $price = intval($gift['price']);
        if($old_price * $old_num < $gift_num * $price){
            $this->model_room_hostmicro->set_gift($room_id,  $user['user_id'], $user['nickname'], $user_image, $gift_id, $gift['name'], $gift['image'], $gift_num, intval($gift['price']));
            return true;
        }
        return false;
    }


    /**
     * 取消设置礼物
     * @param $room_id
     * @return bool
     */
    public function unmark_gift($room_id){
        $gift_data = $this->model_room_hostmicro->get_gift($room_id);
        if (!empty($gift_data)){
            $this->model_room_hostmicro->del_gift($room_id);
            return true;
        }
        return false;
    }

    /**获取主持麦user_id
     * @param $room_id
     * @return int|null
     */
    public function getHostMicUser($room_id){
        return $this->model_room_hostmicro->get_user_id($room_id);

    }

}